package com.klangner.balls
{
  
  import flash.events.Event;
  import flash.geom.Rectangle;
  
  import mx.containers.Canvas;

  public class BallsView extends Canvas
  {
    private static var MAX_BALLS:int = 10;

    private var balls:Array = new Array();

    public function init():void{
      var ball:Ball;
      var startX:Number;
      var startY:Number;
      var cd:CollisionDetector = new CollisionDetector(new Rectangle(0, 0, width, height), balls);
      
      for(var i:int = 0; i < MAX_BALLS; i++){
        startX = 50 + (Math.random() * (width-100));
        startY = 50 + (Math.random() * (height-100));
        ball = new Ball(startX, startY, cd);
        ball.radius = Math.random()*45 + 5;
        ball.setColor(Math.random()*200+55, Math.random()*200+55, Math.random()*200+55); 
        ball.setVelocity(Math.random()*10-5, Math.random()*4-2);
        balls.push(ball);
        this.rawChildren.addChild(ball);
      }

      this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }
    
    private function onEnterFrame(event:Event):void {

      for each(var ball:Ball in balls){
        ball.move();
      }
    }

  }
}